//
//  ZLBurnAfterReadingCellContentView.m
//  ZLYoYoIM_Example
//
//  Created by 赵磊 on 2021/1/20.
//  Copyright © 2021 itzhaolei. All rights reserved.
//

#import "ZLBurnAfterReadingCellContentView.h"
#import "ZLAttachment.h"
#import <UIImageView+WebCache.h>
#import "UIImage+ZLIMCustom.h"
#import "UIImageView+ZLBlurredImage.h"
#import "ZLBurnAfterReadingImageView.h"
#import <NIMKit/NIMKit.h>
#import "UIImage+ZLIMCustom.h"

@implementation ZLBurnAfterReadingCellContentView

- (instancetype)initSessionMessageContentView{
    if (self = [super initSessionMessageContentView]) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0.5, 120.0, 120.0)];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.layer.cornerRadius = 5.0;
        imageView.layer.masksToBounds = YES;
        [self addSubview:imageView];
        self.unitView = imageView;
        UIImageView *markImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-1.0, -1.0, 122.0, 122.0)];
        markImageView.layer.cornerRadius = 5.0;
        markImageView.layer.masksToBounds = YES;
        markImageView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.1];
        [self addSubview:markImageView];
        self.markImageView = markImageView;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 95.0, 120.0, 20.0)];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:14.0];
        [imageView addSubview:label];
        self.titleLabel = label;
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]];
    }
    return self;
}

- (void)refresh:(NIMMessageModel*)data{
    //务必调用super方法
    [super refresh:data];
    NIMCustomObject *object = data.message.messageObject;
    ZLAttachment *attachment = object.attachment;
    //识别方向
    __weak typeof(self)weakSelf = self;
    [self.unitView sd_setImageWithURL:[NSURL URLWithString:attachment.url] placeholderImage:nil options:0 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        weakSelf.image = image;
        [weakSelf.unitView setImageToBlur:image blurRadius:30 completionBlock:nil];
    }];
    
    BOOL isDidLook = [data.message.localExt[@"fire"] boolValue];
    
    self.titleLabel.text = !isDidLook ? @"点击查看" : @"已焚烧";
    self.titleLabel.textColor = !isDidLook ? [UIColor colorWithRed:245/255.0 green:74/255.0 blue:68/255.0 alpha:1.0] : [UIColor colorWithRed:159/255.0 green:159/255.0 blue:159/255.0 alpha:1.0];
    UIImage *image = [UIImage imageNamedInIM:@"huo 拷贝 2"];
    UIImage *didLookImage = [UIImage imageNamedInIM:@"huo 拷贝"];
    self.markImageView.image = isDidLook ? didLookImage : image;
    
}

- (void)tapAction {
    if (!self.unitView.image) {
        return;
    }
    if ([self.model.message.localExt[@"fire"] intValue]) {
        return;
    }
    NIMCustomObject *object = self.model.message.messageObject;
    ZLAttachment *attachment = object.attachment;
    if (attachment.url.length < 1) {
        return;
    }
    __weak typeof(self)weakSelf = self;
    [ZLBurnAfterReadingImageView show:self.unitView Image:self.unitView.image HDImage:self.image Time:3.0 DismissAction:^{
        NSMutableDictionary *localExt = [NSMutableDictionary dictionaryWithDictionary:weakSelf.model.message.localExt];
        localExt[@"fire"] = @"1";
        weakSelf.model.message.localExt = localExt;
        [[NIMSDK sharedSDK].conversationManager updateMessage:weakSelf.model.message forSession:weakSelf.model.message.session completion:^(NSError * _Nullable error) {
            [weakSelf refresh:weakSelf.model];
        }];
    }];
}

@end
